package com.dragon8869.myspringboot.service.impl;

import com.dragon8869.myspringboot.annotation.LogExecuteTime;
import com.dragon8869.myspringboot.dao.AyUserDao;
import com.dragon8869.myspringboot.entity.AyUser;
import com.dragon8869.myspringboot.repository.AyUserRepository;
import com.dragon8869.myspringboot.service.AyUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Service
public class AyUserServiceImpl implements AyUserService {

    @Resource
    private AyUserRepository ayUserRepository;

    @Resource
    AyUserDao ayUserDao;

    @Resource
    private RedisTemplate<String, Map<String, AyUser>> redisTemplate;

    /*@Autowired
    public AyUserServiceImpl(AyUserRepository ayUserRepository) {
        this.ayUserRepository = ayUserRepository;
    }*/

    private final static String USER_BY_ID = "USER_BY_ID";

    @Override
    public AyUser findById(Integer id) {
        AyUser ayUser = (AyUser) redisTemplate.opsForHash().get(USER_BY_ID, id);
        if (ayUser == null) {
            ayUser = ayUserRepository.findById(id).orElse(null);
            if (ayUser != null) {
                redisTemplate.opsForHash().put(USER_BY_ID, id, ayUser);
            }
        }
        return ayUser;
        /*Optional<AyUser> byId = ayUserRepository.findById(id);
        return byId.orElse(null);*/
    }

    @Override
    @LogExecuteTime
    public List<AyUser> findAll() {
        return ayUserRepository.findAll();
    }

    @Override
    @Transactional
    public AyUser save(AyUser ayUser) {
//        AyUser saveUser = ayUserRepository.save(ayUser);
//        String error = null;
//        error.split("/");
//        return saveUser;
        return ayUserRepository.save(ayUser);
    }

    @Override
    public void delete(Integer id) {
        ayUserRepository.deleteById(id);
    }

    @Override
    public Page<AyUser> findAll(Pageable pageable) {
        return ayUserRepository.findAll(pageable);
    }

    @Override
    public AyUser findByName(String name) {
        return ayUserRepository.findByName(name);
    }

    @Override
    public List<AyUser> findByNameLike(String name) {
        return ayUserRepository.findByNameLike(name);
    }

    @Override
    public List<AyUser> findByIdIn(Collection<Integer> ids) {
        return ayUserRepository.findByIdIn(ids);
    }

    @Override
    public AyUser findByNameAndPassword(String name, String password) {
        return ayUserDao.findByNameAndPassword(name, password);
    }

    @Override
    public AyUser createUser(AyUser ayUser) {
        ayUserDao.createUser(ayUser);
        return ayUser;
    }

    @Override
    public AyUser updateUser(AyUser ayUser) {
        ayUserDao.updateUser(ayUser);
        return ayUserRepository.findById(ayUser.getId()).orElse(null);
    }

    @Override
    public AyUser deleteUser(Integer id) {
        AyUser ayUser = ayUserRepository.findById(id).orElse(null);
        ayUserDao.deleteUser(id);
        return ayUser;
    }

    @Override
    @Async
    @LogExecuteTime
    public Future<List<AyUser>> findAsynAll() {
        List<AyUser> all = ayUserRepository.findAll();
        return new AsyncResult<>(all);
    }

}
